package com.ebeitech.doorapp.qqim.util;

import android.app.NotificationManager;

import com.ebeitech.doorapp.application.EbeiApplication;
import com.ebeitech.doorapp.domain.dao.EMessageDao;
import com.ebeitech.doorapp.qqim.model.Message;
import com.ebeitech.doorapp.qqim.model.MessageFactory;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMGroupReceiveMessageOpt;
import com.tencent.imsdk.TIMMessage;
import com.tencent.qcloud.presentation.event.MessageEvent;

import org.json.JSONObject;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by alice on 2017/11/3.
 */
public class PushUtil implements Observer {

	private static final String TAG = PushUtil.class.getSimpleName();

	private static int pushNum=0;

	private final int pushId=1;

	private static PushUtil instance = new PushUtil();

	private EMessageDao mMessageDao;

	private PushUtil() {
		MessageEvent.getInstance().addObserver(this);
	}

	public static PushUtil getInstance(){
		return instance;
	}



	private void PushNotify(TIMMessage msg){
		//系统消息，自己发的消息，程序在前台的时候不通知
		if (msg==null/*||Foreground.get().isForeground()*/||
				(msg.getConversation().getType()!= TIMConversationType.Group&&
						msg.getConversation().getType()!= TIMConversationType.C2C)||
				/*msg.isSelf()||*///目前APP的消息是由后台发送的
				msg.getRecvFlag() == TIMGroupReceiveMessageOpt.ReceiveNotNotify/* ||
				MessageFactory.getMessage(msg) instanceof CustomMessage*/) return;

		String senderStr,contentStr;
		Message message = MessageFactory.getMessage(msg);
		if (message == null) return;
		senderStr = message.getSender();
		contentStr = message.getSummary();
		com.ebeitech.library.util.Log.d(TAG, "recv msg = " + contentStr);

		String messageType = "";
		try {
			JSONObject jsonObject = new JSONObject(contentStr);
			messageType = jsonObject.optString("messageType");
		} catch (Exception e) {}

		mMessageDao = EMessageDao.getInstance();
		mMessageDao.handlerMessage(contentStr);

		/*NotificationManager mNotificationManager = (NotificationManager) MyApplication.getContext().getSystemService(MyApplication.getContext().NOTIFICATION_SERVICE);
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MyApplication.getContext());
		Intent notificationIntent = new Intent(MyApplication.getContext(), HomeActivity.class);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(MyApplication.getContext(), 0,
				notificationIntent, 0);
		mBuilder.setContentTitle(senderStr)//设置通知栏标题
				.setContentText(contentStr)
				.setContentIntent(intent) //设置通知栏点击意图
//                .setNumber(++pushNum) //设置通知集合的数量
				.setTicker(senderStr+":"+contentStr) //通知首次出现在通知栏，带上升动画效果的
				.setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
				.setDefaults(Notification.DEFAULT_ALL)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
				.setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON
		Notification notify = mBuilder.build();
		notify.flags |= Notification.FLAG_AUTO_CANCEL;
		mNotificationManager.notify(pushId, notify);*/
	}

	public static void resetPushNum(){
		pushNum=0;
	}

	public void reset(){
		NotificationManager notificationManager = (NotificationManager) EbeiApplication.getContext().getSystemService(EbeiApplication.getContext().NOTIFICATION_SERVICE);
		notificationManager.cancel(pushId);
	}

	/**
	 * This method is called if the specified {@code Observable} object's
	 * {@code notifyObservers} method is called (because the {@code Observable}
	 * object has been updated.
	 *
	 * @param observable the {@link Observable} object.
	 * @param data       the data passed to {@link Observable#notifyObservers(Object)}.
	 */
	@Override
	public void update(Observable observable, Object data) {
		if (observable instanceof MessageEvent){
			if (data instanceof TIMMessage) {
				TIMMessage msg = (TIMMessage) data;
				if (msg != null){
					PushNotify(msg);
				}
			}
		}
	}
}